package com.crm.marketing.service;

import com.crm.common.mapper.PlanMapper;
import com.crm.common.pojo.Plan;
import com.crm.global.exception.CrmException;
import com.crm.global.exception.ExceptionEnum;
import com.crm.id.increment.IdWorker;
import com.crm.mybatis.separation.Read;
import com.crm.mybatis.separation.Write;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PlanService {

    @Autowired
    private PlanMapper planMapper;

    @Write
    public void saveOrUpdate(Plan plan) {
        if(plan == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }

        if (plan.getId() == null) {
            plan.setId(new IdWorker().nextId());
            planMapper.insert(plan);
        } else {
             planMapper.updateByPrimaryKey(plan);
        }
    }

    @Read
    public Plan findById(Long id) {
        if (id == null) {
            throw  new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        return planMapper.selectByPrimaryKey(id);
    }

}
